<?php 
if (!defined("LEVEL") || LEVEL < 30 || !defined("OK")) { header('location: ?home'); }
?>
<script>
// Balsavimui
var arrInput = new Array(0);
  var arrInputValue = new Array(0);

function addInput() {
  arrInput.push(arrInput.length);
  arrInputValue.push("");
  display();
}

function display() {
  document.getElementById('parah').innerHTML="";
  for (intI=0;intI<arrInput.length;intI++) {
    document.getElementById('parah').innerHTML+=createInput(arrInput[intI], arrInputValue[intI]);
  }
}

function saveValue(intId,strValue) {
  arrInputValue[intId]=strValue;
}

function createInput(id,value) {
  return "<input type='text' name='"+ id +"' onChange='javascript:saveValue("+ id +",this.value)' value='"+ value +"'><br><br>";
}

function deleteInput() {
  if (arrInput.length > 0) {
     arrInput.pop();
     arrInputValue.pop();
  }
  display();
}
</script>
<h2>Balsavimo administravimas</h2>
<table border=0 width=100%>
	<tr>
		<td valign=top>
			<?php
				$text = "
				<form name=\"b_list\" action=\"?id,46;a,5\" method=\"post\">
					<select size=\"1\" name=\"b_edit_del\">
				";
				$sql = mysql_query1("SELECT id,info FROM balsavimas WHERE ijungtas='NE'");
				while($row = mysql_fetch_assoc($sql)) {
					$info = explode(";", $row['info']);
					$text .= "<option value=" . $row['id'] .">" . $info[0] ."</option>";
				}
				$text .="
					</select>
					<input name=\"b_delete\" type=\"submit\" value=\"Trinti\">
				</form>
				";
				lentele("Balsavimų istorija",$text);
				unset($sql,$row,$text,$info);
				$sql = mysql_fetch_assoc(mysql_query1("SELECT info, id FROM balsavimas WHERE ijungtas='TAIP'"));
				$info = explode(";",$sql['info']);
				$ats1 = explode("-",$info[1]);
				$ats2 = explode("-",$info[2]);
				$ats3 = explode("-",$info[3]);
				$ats4 = explode("-",$info[4]);
				$ats5 = explode("-",$info[5]);
				$text = "
					<table border=0>
						<tr>
							<td width=10%>Klausimas:</td>
							<td><b>" . $info[0] ."</b></td>
						</tr>
						<tr>
							<td></td>
							<td></td>
						</tr>
						<tr>
							<td>" . $ats1[0] ."</td>
							<td>" . $ats1[1] ."</td>
						</tr>
						<tr>
							<td>" . $ats2[0] ."</td>
							<td>" . $ats2[1] ."</td>
						</tr>
						<tr>
							<td>" . $ats3[0] ."</td>
							<td>" . $ats3[1] ."</td>
						</tr>
						<tr>
							<td>" . $ats4[0] ."</td>
							<td>" . $ats4[1] ."</td>
						</tr>
						<tr>
							<td>" . $ats5[0] ."</td>
							<td>" . $ats5[1] ."</td>
						</tr>
					</table>
				";
				lentele("Aktyvus balsavimas",$text);
				unset($text,$a,$total,$info2,$info,$sql);
			?>
		</td>
		<td valign=top>
			<?php
			if (isset($_POST['b_create']) && $_POST['b_create'] == 'Sukurti') {
				$ats1 = $_POST[0];
				$ats2 = $_POST[1];
				$ats3 = $_POST[2];
				$ats4 = $_POST[3];
				$ats5 = $_POST[4];
				$kl = $_POST['b_kl'];
				$balsas = $kl .";";
				if (!empty($ats1)) { $balsas .= $ats1 ."-0;"; }
				if (!empty($ats2)) { $balsas .= $ats2 ."-0;"; }
				if (!empty($ats3)) { $balsas .= $ats3 ."-0;"; }
				if (!empty($ats4)) { $balsas .= $ats4 ."-0;"; }
				if (!empty($ats5)) { $balsas .= $ats5 ."-0;"; }
				mysql_query1("INSERT INETO balsavimas (info, autorius, laikas) VALUES (
							'" . $balsas ."', '" . $_SESSION['id'] ."', '" . date("Y-m-d H:i:s") ."')");
				header("Location: ?id,46;a,5");
			}
			unset($a,$ats1,$ats2,$ats3,$ats4,$ats5,$balsas,$sujungti);
				$text = "
					<form name=\"b_create\" action=\"?id,46;a,5\" method=\"post\">
						<table border=0>
							<tr>
								<td width=10%>Klausimas:</td>
								<td><input name=\"b_kl\" type=\"text\" size=50 value=\"\"></td>
							</tr>
							<tr>
								<td></td>
								<td>
									<p id=\"parah\"></p>
									<a href=\"javascript:addInput()\"><img src=\"images/balsavimas/plus.gif\" alt=\"Pridėti\" border=\"0\" /></img></a>
									<a href=\"javascript:deleteInput()\"><img src=\"images/balsavimas/minus.gif\" alt=\"Atimti\" border=\"0\" /></img></a>
								</td>
							</tr>
							</table>
							<input name=\"b_create\" type=\"submit\" value=\"Sukurti\"><br>
							<b>Priminimas:</b> Galima daugiausia 5 atsakymai. Pasirinkus naują atsakymą senieji dingsta. Prieš įkeliant atsakymus sugalvokite kiek jų bus.
					</form>
				";
				lentele("Naujas balsavimas",$text);
				unset($text);
			?>
		</td>
	</tr>
</table>